package com.tushen.task;


import com.tushen.utils.SqlSessionFactoryUtil;

import java.io.File;

/**
 * @author likun
 * @version 1.0
 * @date 2020/12/12 9:03 下午
 */
public class NCThread extends Thread{

    String fileName;
    String outFilePath;
    String tableName;
    String taskid;
    String version;

    public NCThread(String fileName,String taskid,String version){
        this.fileName=fileName;
        this.tableName="nc_shp_result_"+version;
//        this.outFilePath="/Users/likun/Downloads/tmp/"+tableName+".csv";
        this.outFilePath="D:/tmp/"+tableName+".csv";
        this.taskid=taskid;
        this.version=version;
    }

    public void run(){
        File file=new File(fileName);
        int status=file.exists()?2:0;
        if(status==2){NC.processNC(fileName,outFilePath,tableName);}

        String updateTaskSql="update data_process_task set end_dtm=now(),status="+status+" where taskid='"+taskid+"'";
        SqlSessionFactoryUtil.excuteUpdateSql(updateTaskSql);
        String updateVersionSql=
                "with a as (SELECT min(dt) as min_dt,max(dt) as max_dt FROM "+tableName+")\n" +
                ",b as (\n" +
                "SELECT  min(tm) as min_tm  from  "+tableName+" where dt = (SELECT min_dt from a))\n" +
                ",c as (\n" +
                "SELECT  max(tm) as max_tm  from "+tableName+" where dt = (SELECT max_dt from a))\n" +
                "\n" +
                "insert into data_dtm_verion(m_type,m_version,start_dtm,end_dtm) " +
                 "SELECT 'uv',"+version+", (min_dt+min_tm)::TIMESTAMP as start_dtm,(max_dt+max_tm)::TIMESTAMP as end_dtm from a,b,c " +
                        " on  CONFLICT(m_type,m_version) do nothing";
        if(status==2){
            SqlSessionFactoryUtil.excuteUpdateSql(updateVersionSql);
        }




    }
}
